package cn.krund.lyc.hotlerobot.activity.ui;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import cn.krund.lyc.hotlerobot.R;
import cn.krund.lyc.hotlerobot.activity.base.BaseActivity;
import cn.krund.lyc.hotlerobot.activity.view.BatteryView;
import cn.krund.lyc.hotlerobot.Ros.common.entity.enums.RobotErrorInfo;
import cn.krund.lyc.hotlerobot.Ros.common.entity.enums.RobotFeedbackInfoType;
import cn.krund.lyc.hotlerobot.Ros.service.ICommunicationListener;
import cn.krund.lyc.hotlerobot.service.RemoteControlService;
import cn.krund.lyc.hotlerobot.service.RemoteControlServiceBinder;
import cn.krund.lyc.hotlerobot.volumemanage.VolumeDialog;

/**
 * @包名: cn.krund.lyc.hotlerobot.activity.ui
 * @作者: 刘育成
 * @时间: 2018/3/22  9:51
 * @org: 克路德人工智能
 * @描述: TODO
 * @fileName: HomeRobotActivity
 */

public class HomeRobotActivity extends BaseActivity implements View.OnClickListener {
    private BatteryView battery_view;
    private TextView tv_battery;
    private LinearLayout ll_wifi, ll_volume, ll_setting;
    private RemoteControlService remoteControlService;
    private ImageView iv_battery_pattern, iv_delivery_pattern, iv_mapping_pattern;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bindRemoteControlService();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_home_robot;
    }

    @Override
    protected void initView() {
        tv_battery = (TextView) findViewById(R.id.tv_battery);
        battery_view = (BatteryView) findViewById(R.id.battery_view);
        ll_wifi = (LinearLayout) findViewById(R.id.ll_wifi);
        ll_volume = (LinearLayout) findViewById(R.id.ll_volume);
        ll_setting = (LinearLayout) findViewById(R.id.ll_setting);
        iv_battery_pattern = (ImageView) findViewById(R.id.iv_battery_pattern);
        iv_delivery_pattern = (ImageView) findViewById(R.id.iv_delivery_pattern);
        iv_mapping_pattern = (ImageView) findViewById(R.id.iv_mapping_pattern);
        ll_wifi.setOnClickListener(this);
        ll_setting.setOnClickListener(this);
        ll_volume.setOnClickListener(this);
        iv_battery_pattern.setOnClickListener(this);
        iv_delivery_pattern.setOnClickListener(this);
        iv_mapping_pattern.setOnClickListener(this);
        battery_view.setPower(100);
        tv_battery.setText(10 + "%");
    }

    @Override
    protected void initNev() {

    }

    private void bindRemoteControlService() {
        Intent intent = new Intent(this, RemoteControlService.class);
        this.bindService(intent, remoteControlServiceConn, BIND_AUTO_CREATE);
    }

    private ServiceConnection remoteControlServiceConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            remoteControlService = ((RemoteControlServiceBinder) service).getService();
            remoteControlService.AddCommunicationListener(rosListener);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            remoteControlService = null;
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_mapping_pattern://领路
                openActivity(StartWayPatternActivity.class);

                break;
            case R.id.iv_battery_pattern://充电

                break;
            case R.id.iv_delivery_pattern://送货
                openActivity(DeliveryPatternActivity.class);
                break;
            case R.id.ll_wifi:
                openActivity(WifiFunctionActivity.class);
                break;
            case R.id.ll_volume:
                VolumeDialog.Builder alert = new VolumeDialog.Builder(this);
                VolumeDialog dialog = alert.show();
                //设置Dialog 尺寸
                dialog.setDialogWindowAttr(0.7, 0.3, this);
                break;
            case R.id.ll_setting:
                openActivity(PasswordActivity.class);
                break;
        }
    }

    /**
     * 通讯
     */
    ICommunicationListener rosListener = new ICommunicationListener() {
        @Override
        public void onConnectComplete() {

        }

        @Override
        public void onDisconnect() {

        }

        @Override
        public void onFeedback(RobotFeedbackInfoType feedbackType, boolean isSucceed) {

        }

        @Override
        public void onFeedback(RobotFeedbackInfoType feedbackType, Object feedbackInfo) {

        }

        @Override
        public void onFeedback(RobotFeedbackInfoType feedbackType, Object feedbackInfo, boolean isSucceed) {

        }

        @Override
        public void onRobotRealTimePos(float pointX, float pointY, float courseAngle) {

        }

        @Override
        public void onOSPFInfo(byte[] data) {

        }

        @Override
        public void onErrorInfo(RobotErrorInfo robotErrorInfo) {

        }
    };

    @Override
    protected void onDestroy() {
        this.unbindService(remoteControlServiceConn);
        super.onDestroy();

    }

    /***************** 双击退出程序 ************************************************/
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (KeyEvent.KEYCODE_BACK == keyCode) {
            // 判断是否在两秒之内连续点击返回键，是则退出，否则不退出
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                // 将系统当前的时间赋值给exitTime
                exitTime = System.currentTimeMillis();
            } else {
                finishAll();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
